//	Copyright (C) 2010  Thomas Andersen (thomas.1.andersen@gmail.com)
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//	
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//	
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.	


function CreateUser_callBack(doc)
{
    var parser = new XMLRequestParser(doc);
    if (parser.CheckError())
    {
        alert(parser.GetError());
        document.NewUserForm.Submit.disabled = false;
    }
    else
    {
        var html = "<div id=info>";
        html += "User created, please activate from your mailbox.";
        html += "</div>";
        document.getElementById("maininfo").innerHTML = html;
    }
}


function CreateUser() {
    document.NewUserForm.Submit.disabled = true;
    var pl = new XMLReqParameters();
    var sess = readCookie('OpenSecretSession');
    pl.add("sessionid", sess);
    pl.add("name", document.NewUserForm.userid.value);
    pl.add("password", hex_md5(document.NewUserForm.password.value));
    pl.add("password2", hex_md5(document.NewUserForm.password2.value));
    pl.add("email", document.NewUserForm.email.value);
    XMLReq.invoke("service.php", "CreateUser", pl, false, CreateUser_callBack);
}


function NewUserOpen()
{
    var html = "<form class=newuserform id=\"NewUserForm\" name=\"NewUserForm\" onsubmit=\"CreateUser(); return false;\">";
    html += "<fieldset>";
    html += "<legend>New User</legend>";
    html += "<input id=\"user\" name=\"user\" type=\"hidden\" value=\"\"/>";
    html += "<input id=\"pass\" name=\"pass\" type=\"hidden\" value=\"\"/>";
    html += "<label for=userid>Username:</label><input class=formitem type=\"text\" id=\"userid\" name=\"userid\" size=\"20\" maxlength=45/><br/>";
    html += "<label for=email>Email:</label><input class=formitem type=\"text\" id=\"email\" name=\"email\" size=\"20\" maxlength=200/><br/>";
    html += "<label for password>Password:</label><input class=formitem type=password id=\"password\" name=password size=20 maxlength=20><br/>";
    html += "<label for password2>Repeat:</label><input class=formitem type=password id=\"password2\" name=password2 size=20 maxlength=20><br/>";
    html += "<input class=formitembutton type=submit name=Submit value=Create user>";
    html += "</fieldset>";
    html += "</form>";
    document.getElementById("mainform").innerHTML = html;
	document.getElementById("maininfo").innerHTML = "";
	document.NewUserForm.userid.focus();
}

